package com.snow.auth.service;

import com.snow.exception.CommonRuntimeException;
import com.snow.jpa.BaseService;
import com.snow.auth.entity.SystemRole;
import com.snow.auth.repository.SystemRoleRepository;
import com.snow.auth.repository.SystemUserRoleRepository;
import lombok.Getter;
import org.springframework.stereotype.Service;

/**
 * 系统角色管理
 *
 * @author by 13119
 */
@Getter
@Service
public class SystemRoleService extends BaseService<SystemRoleRepository, SystemRole, Integer> {
    final SystemUserRoleRepository systemUserRoleRepository;

    public SystemRoleService(SystemRoleRepository repository, SystemUserRoleRepository systemUserRoleRepository) {
        super(repository);
        this.systemUserRoleRepository = systemUserRoleRepository;
    }

    @Override
    public SystemRole delete(Integer id) {
        long count = systemUserRoleRepository.lambdaQuery().equal("roleId", id).count();
        if (count > 0) {
            throw new CommonRuntimeException("该角色已被使用，请勿删除");
        }
        return super.delete(id);
    }
}
